#pragma once

class LayerUI
{
	Dialog* m_dialog;
	DialogText* m_noTile;
	DialogText* m_layerText;
	DialogText* m_textureName;
	DialogText* m_desc;
	DialogText* m_desc2;
	DialogText* m_doodadId;
	UIButton<LayerUI>* m_nextLayer;
	UIButton<LayerUI>* m_prevLayer;

	ui32 m_numLayers, m_curLayer;
	ADTChunk* m_curChunk;

	Checkbox<LayerUI>* m_rot45;
	Checkbox<LayerUI>* m_rot90;
	Checkbox<LayerUI>* m_rot180;
	Checkbox<LayerUI>* m_animFast;
	Checkbox<LayerUI>* m_animFaster;
	Checkbox<LayerUI>* m_animFastest;

	EditBox* m_effectId;
	UIButton<LayerUI>* m_sEffect;

	void TextureButton(UIButton<LayerUI>*)
	{
		m_dialog->m_visible = !m_dialog->m_visible;
	}

	void NextLayer(UIButton<LayerUI>*)
	{
		++m_curLayer;
		if(m_curLayer >= m_numLayers)
		{
			m_nextLayer->m_enabled = false;
			m_curLayer = m_numLayers;
		}
		else
			m_nextLayer->m_enabled = true;

		if(m_curLayer > 1)
			m_prevLayer->m_enabled = true;

		char msg[255];
		sprintf_s<255>(msg, "Layer: %u / %u", m_curLayer, m_numLayers);
		m_layerText->szText = msg;
		if(m_curChunk)
		{
			m_textureName->szText = m_curChunk->GetTextureName(m_curLayer - 1);
			TileTexLayer& l = m_curChunk->GetTexLayer(m_curLayer - 1);
			if(l.flags & 0x01)
				m_rot45->SetChecked(1);
			else
				m_rot45->SetChecked(0);
			if(l.flags & 0x02)
				m_rot90->SetChecked(1);
			else
				m_rot90->SetChecked(0);
			if(l.flags & 0x04)
				m_rot180->SetChecked(1);
			else
				m_rot180->SetChecked(0);
			if(l.flags & 0x08)
				m_animFast->SetChecked(1);
			else
				m_animFast->SetChecked(0);
			if(l.flags & 0x10)
				m_animFaster->SetChecked(1);
			else
				m_animFaster->SetChecked(0);
			if(l.flags & 0x20)
				m_animFastest->SetChecked(1);
			else
				m_animFastest->SetChecked(0);
			GroundEffectTexture* d = dbcGroundTexture.LookupEntry(l.effectId);
			if(!d)
				m_doodadId->szText = "ID: -1 Name: No doodads set!";
			else
			{
				GroundEffectDoodad* dd = dbcGroundEffect.LookupEntry(d->refs[0]);
				if(!dd)
					m_doodadId->szText = "ID: -1 Name: No doodads set!";
				else
				{
					char msg[255];
					sprintf_s<255>(msg, "Id: %u  Name: %s", l.effectId, dd->name);
					m_doodadId->szText = msg;
				}
			}
		}
	}

	void PrevLayer(UIButton<LayerUI>*)
	{
		--m_curLayer;
		if(m_curLayer <= 1)
		{
			m_prevLayer->m_enabled = false;
			m_curLayer = 1;
		}
		else
			m_prevLayer->m_enabled = true;
		
		if(m_curLayer < m_numLayers)
			m_nextLayer->m_enabled = true;

		char msg[255];
		sprintf_s<255>(msg, "Layer: %u / %u", m_curLayer, m_numLayers);
		m_layerText->szText = msg;
		if(m_curChunk)
		{
			m_textureName->szText = m_curChunk->GetTextureName(m_curLayer - 1);
			TileTexLayer& l = m_curChunk->GetTexLayer(m_curLayer - 1);
			if(l.flags & 0x01)
				m_rot45->SetChecked(1);
			else
				m_rot45->SetChecked(0);
			if(l.flags & 0x02)
				m_rot90->SetChecked(1);
			else
				m_rot90->SetChecked(0);
			if(l.flags & 0x04)
				m_rot180->SetChecked(1);
			else
				m_rot180->SetChecked(0);
			if(l.flags & 0x08)
				m_animFast->SetChecked(1);
			else
				m_animFast->SetChecked(0);
			if(l.flags & 0x10)
				m_animFaster->SetChecked(1);
			else
				m_animFaster->SetChecked(0);
			if(l.flags & 0x20)
				m_animFastest->SetChecked(1);
			else
				m_animFastest->SetChecked(0);
			GroundEffectTexture* d = dbcGroundTexture.LookupEntry(l.effectId);
			if(!d)
				m_doodadId->szText = "ID: -1 Name: No doodads set!";
			else
			{
				GroundEffectDoodad* dd = dbcGroundEffect.LookupEntry(d->refs[0]);
				if(!dd)
					m_doodadId->szText = "ID: -1 Name: No doodads set!";
				else
				{
					char msg[255];
					sprintf_s<255>(msg, "Id: %u  Name: %s", l.effectId, dd->name);
					m_doodadId->szText = msg;
				}
			}
		}
	}

	void LayerFlagClick(Checkbox<LayerUI>* pBox, ui32 state)
	{
		if(!m_curChunk)
			return;
		TileTexLayer& t = m_curChunk->GetTexLayer(m_curLayer - 1);
		if(state)
			t.flags |= (ui32)pBox->pData;
		else
			t.flags &= ~(ui32)pBox->pData;
		if(t.flags & 0x1 || t.flags & 0x2 || t.flags & 0x4 || t.flags & 0x8 || t.flags & 0x10 || t.flags & 0x20)
			t.flags |= 0x40;
		else
			t.flags &= ~ui32(0x40);
		sD3D.AddChanged(m_curChunk->GetParent());
		if(state)
		{
			if(pBox == m_rot45)
			{
				m_rot90->SetChecked(0);
				LayerFlagClick(m_rot90, 0);
				m_rot180->SetChecked(0);
				LayerFlagClick(m_rot180, 0);
			}
			else if(pBox == m_rot90)
			{
				m_rot45->SetChecked(0);
				LayerFlagClick(m_rot45, 0);
				m_rot180->SetChecked(0);
				LayerFlagClick(m_rot180, 0);
			}
			else if(pBox == m_rot180)
			{
				m_rot45->SetChecked(0);
				LayerFlagClick(m_rot45, 0);
				m_rot90->SetChecked(0);
				LayerFlagClick(m_rot90, 0);
			}
			else if(pBox == m_animFast)
			{
				m_animFaster->SetChecked(0);
				m_animFastest->SetChecked(0);
				LayerFlagClick(m_animFaster, 0);
				LayerFlagClick(m_animFastest, 0);
			}
			else if(pBox == m_animFaster)
			{
				m_animFast->SetChecked(0);
				m_animFastest->SetChecked(0);
				LayerFlagClick(m_animFast, 0);
				LayerFlagClick(m_animFastest, 0);
			}
			else if(pBox == m_animFastest)
			{
				m_animFast->SetChecked(0);
				m_animFaster->SetChecked(0);
				LayerFlagClick(m_animFast, 0);
				LayerFlagClick(m_animFaster, 0);
			}
		}
	}

	void SetEffectDoodad(UIButton<LayerUI>*);

public:
	LayerUI(ToolBar* pToolBar);
	void CheckChunk(Vertex3F& pos);
	void Update();
};